package com.zzc.emotisenseclassroom;

import com.alibaba.fastjson2.JSON;
import com.zzc.config.ZzcConfig;
import com.zzc.emotisenseclassroom.dbservice.EmotisenseScore.EmotisenseScoreDbService;
import com.zzc.emotisenseclassroom.dto.emotisenseScore.EmotisenseScoreDTO;
import com.zzc.emotisenseclassroom.dto.user.StudentDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@SpringBootApplication
@Import(ZzcConfig.class)
public class EmotiSenseClassroomApplication {
    public static void main(String[] args) {
        SpringApplication.run(EmotiSenseClassroomApplication.class, args);
    }

}

@Component
class DataGenerator implements CommandLineRunner {

    @Autowired
    private EmotisenseScoreDbService emotiSenseScoreRepository;
    @Autowired
    private  StudentScoreGenerator studentScoreGenerator;

    private Random random = new Random();

    private static boolean needInsertScore = false;

    @Override
    public void run(String... args) {
        if (needInsertScore){
            studentScoreGenerator.doInsertScore();
        }


        /*// 学生信息列表（您提供的 JSON 信息转化为 List<StudentInfo>）
        List<StudentInfo> studentInfos = getStudentInfos();

        // 开始和结束日期
        LocalDate startDate = LocalDate.of(2022, 5, 1);
        LocalDate endDate = LocalDate.of(2026, 10, 31);

        // 遍历所有学生
        for (StudentInfo studentInfo : studentInfos) {
            generateScoresForStudent(studentInfo, startDate, endDate);
        }*/
    }

    private void generateScoresForStudent(StudentInfo studentInfo, LocalDate startDate, LocalDate endDate) {
        LocalDate currentDate = startDate;

        // 每天遍历日期范围
        while (!currentDate.isAfter(endDate)) {
            // 随机生成 0 到 5 条评分数据
            int numScores = random.nextInt(6);

            List<EmotisenseScoreDTO> scores = new ArrayList<>();

            // 生成评分数据
            for (int i = 0; i < numScores; i++) {
                int score = generateRandomScore();
                EmotisenseScoreDTO scoreEntry = new EmotisenseScoreDTO();
                scoreEntry.setSchoolId(studentInfo.getSchoolId());
                scoreEntry.setClassId(studentInfo.getClassId());
                scoreEntry.setUserId(studentInfo.getUserId());
                scoreEntry.setClassName("20级计科一班");
                scoreEntry.setSchoolName("江苏科技大学");
                scoreEntry.setRealName(studentInfo.getRealName());
                scoreEntry.setScore(score);
                Date date = Date.from(currentDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
                scoreEntry.setClassDate(date);

                scores.add(scoreEntry);
            }

            // 将评分数据插入数据库
            emotiSenseScoreRepository.insertScore(scores);

            // 增加一天
            currentDate = currentDate.plusDays(1);
        }
    }

    private int generateRandomScore() {
        // 确保每个评分段都有数据，并且最低分>50
        int scoreRange = random.nextInt(4);
        int score = 0;

        switch (scoreRange) {
            case 0:
                // 优秀（90-100分）
                score = random.nextInt(11) + 90;
                break;
            case 1:
                // 良好（80-89分）
                score = random.nextInt(10) + 80;
                break;
            case 2:
                // 中等（60-79分）
                score = random.nextInt(20) + 60;
                break;
            case 3:
                // 较差（51-59分）
                score = random.nextInt(9) + 51;
                break;
        }

        return score;
    }

    private List<StudentInfo> getStudentInfos() {
        // 这里是您提供的学生信息的 JSON 数据转化为 Java 对象的方法
        // 您需要根据您实际的学生信息源来编写该方法
        // 这只是一个示例，可以通过解析 JSON 或数据库查询来获取学生信息
        return JSON.parseArray("[{\"classId\":2022107011,\"email\":\"88778877@gmail.com\",\"phoneNumber\":\"887788877\",\"realName\":\"李铁\",\"schoolId\":666666,\"sex\":2,\"userId\":20228398121},{\"classId\":2022107011,\"email\":\"123456@qq.com\",\"phoneNumber\":\"1731536989\",\"realName\":\"鲜大东\",\"schoolId\":666666,\"sex\":0,\"userId\":20224180213213},{\"avatar\":\"https://i0.hdslb.com/bfs/archive/3bb5160123aa7be3c7cd89370d2d79b4024d7d60.jpg\",\"classId\":2022107011,\"email\":\"72043968@qq.com\",\"phoneNumber\":\"123123321\",\"realName\":\"大海\",\"schoolId\":666666,\"sex\":1,\"userId\":17315399988},{\"classId\":2022107011,\"email\":\"7657312@qq.com\",\"phoneNumber\":\"1231111123\",\"realName\":\"小红\",\"schoolId\":666666,\"sex\":1,\"userId\":202241792131},{\"avatar\":\"../../../static/img/avatar.jpg\",\"classId\":2022107011,\"realName\":\"zzc\",\"schoolId\":666666,\"sex\":0,\"userId\":202241806666},{\"classId\":2022107011,\"email\":\"1232111@333.com\",\"phoneNumber\":\"123121309912831\",\"realName\":\"沃特\",\"schoolId\":666666,\"sex\":2,\"userId\":202241491241},{\"avatar\":\"../../../static/img/avatar.jpg\",\"classId\":2022107011,\"email\":\"1397115513@qq.com\",\"phoneNumber\":\"15928736818\",\"realName\":\"\uD83D\uDC3B市委\",\"schoolId\":666666,\"sex\":0,\"userId\":100213213},{\"classId\":2022107011,\"email\":\"23758093@qq.com\",\"phoneNumber\":\"15928688888\",\"realName\":\"小明\",\"schoolId\":666666,\"sex\":1,\"userId\":202241807234},{\"classId\":2022107011,\"email\":\"213123@gmail.com\",\"phoneNumber\":\"12312312412\",\"realName\":\"真智米\",\"schoolId\":666666,\"sex\":0,\"userId\":1231231541},{\"classId\":2022107011,\"email\":\"399123112@123.com\",\"phoneNumber\":\"123141311288\",\"realName\":\"皮克曼\",\"schoolId\":666666,\"sex\":0,\"userId\":2022123813411},{\"classId\":2022107011,\"email\":\"87781@123.com\",\"phoneNumber\":\"88123131\",\"realName\":\"郑智\",\"schoolId\":666666,\"sex\":2,\"userId\":2031231211231}]", StudentInfo.class);
    }
}
